<?php

namespace App\Admin\Controllers;

use App\Models\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class UserController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('用户')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('用户')
            ->description('详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('用户')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);

        $grid->id('Id');
        $grid->name('用户名');
        $grid->mobile('手机号');
        $grid->source('渠道来源');
        $grid->endtime('会员结束时间')->display(function($value){
            return date('Y-m-d H:i:s', $value);
        });
        $grid->created_at('注册时间');

        $grid->disableCreateButton();
        $grid->actions(function(Grid\Displayers\Actions $actions){
            $actions->disableEdit();
        });
        // 增加筛选项
        $grid->filter(function(Grid\Filter $filter) {
            $filter->like('model', '手机号');
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->id('Id');
        $show->name('用户名');
        $show->mobile('手机号');
        $show->source('渠道来源');
        $show->endtime('会员结束时间')->as(function($value){
            return date('Y-m-d H:i:s', $value);
        });
        $show->created_at('创建时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User());

        $form->display('id', 'ID');
        $form->text('name', '用户名')->required();
        $form->text('mobile', '手机号')->required();
        $form->text('source', '来源')->placeholder('请输入来源')->required()->help('产品应该用App来源');
        $form->date('endtime', '会员结束时间')->format('YYYY-MM-DD')->customFormat(function($value) {
            return date('Y-m-d', $value);
        });
        $form->display('created_at', '创建时间');

        return $form;
    }

}
